package com.jxwifi.cloud.quickclean.address;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.idroid.utils.TextDrawable;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.AddressTypeAdapter;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.AddressTypeBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;

import org.apache.http.util.TextUtils;

import java.util.ArrayList;
import java.util.List;

public class AddressPresenter {

    private String TAG = this.getClass().getSimpleName();
    private Context mContext;

    private AddressView mView;
    private AuzImpl mAuz;

    private String[] addtype = {"小区", "写字楼", "学校", "其他"};
    private String[] typeCode = {"xiaoqu", "xiezilou", "xuexiao", "qita"};
    private int mIntItem = 100;//编辑地址所需要的类型的下标(下标在数据实体中获得，如果是添加状态默认为0)

    public AddressPresenter(Context context, AddressView View) {
        this.mContext = context;
        mView = View;
        mAuz = new AuzImpl();
    }


    public void onInitView() {
        final List<AddressTypeBean> mStr = new ArrayList<>();
        for (int i = 0; i < addtype.length; i++) {
            AddressTypeBean mBean = new AddressTypeBean();
            mBean.setTypeName(addtype[i]);
            mBean.setTypenum(i);
            mStr.add(mBean);
        }
        if ("compile".equals(mView.getIsAddOrCompile())) {
            if (StateUtils.XIAO_QU.equals(mView.getAddressBean().getTypeCode())) {
                mView.getLabel1().setBackgroundResource(R.drawable.shap_client_tag_default);
            } else if (StateUtils.XIE_ZI_LOU.equals(mView.getAddressBean().getTypeCode())) {
                mView.getLabel2().setBackgroundResource(R.drawable.shap_client_tag_default);
            } else if (StateUtils.XUE_XIAO.equals(mView.getAddressBean().getTypeCode())) {
                mView.getLabel3().setBackgroundResource(R.drawable.shap_client_tag_default);
            } else if (StateUtils.QI_TA.equals(mView.getAddressBean().getTypeCode())) {
                mView.getLabel4().setBackgroundResource(R.drawable.shap_client_tag_default);
            }
            mView.getConOrderTime().setText(mView.getAddressBean().getProvince() + " " + mView.getAddressBean().getCity() + " " + mView.getAddressBean().getArea());
            mView.getConOrderTimeDetails().setText(mView.getAddressBean().getAddress());
            mView.getEdContent().setText(mView.getAddressBean().getAddressDetail());

        } else {
            if (mView.getIndexSize() == 0) {
                mView.getAddressBean().setIsDefault(StateUtils.YES);
            } else {
                mView.getAddressBean().setIsDefault(StateUtils.NO);
            }
        }

        mView.getLabel1().setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                checkType(StateUtils.XIAO_QU);
                mIntItem = 0;
            }
        });

        mView.getLabel2().setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                checkType(StateUtils.XIE_ZI_LOU);
                mIntItem = 1;
            }
        });

        mView.getLabel3().setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                checkType(StateUtils.XUE_XIAO);
                mIntItem = 2;
            }
        });

        mView.getLabel4().setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                checkType(StateUtils.QI_TA);
                mIntItem = 3;
            }
        });
    }

    //接口请求前的判断
    public void getjudge() {
        if (TextUtils.isEmpty(mView.getConOrderTime().getText().toString())) {
            Toast toast = Toast.makeText(mContext, "地址信息不能为空，请选择", Toast.LENGTH_SHORT);
            ToastUtils.showMyToast(toast, 3000);
            return;
        }
        if (TextUtils.isEmpty(mView.getEdContent().getText().toString())) {
            Toast toast = Toast.makeText(mContext, "详细地址不能为空，请填写", Toast.LENGTH_SHORT);
            ToastUtils.showMyToast(toast, 3000);
            return;
        }

        if (mIntItem == 100) {
            Toast toast = Toast.makeText(mContext, "请选择服务类型", Toast.LENGTH_SHORT);
            ToastUtils.showMyToast(toast, 3000);
            return;
        }
        mView.getAddressBean().setAddress(mView.getAddressBean().getAddressMap() + "" + mView.getEdContent().getText().toString());
        mView.getAddressBean().setAddressDetail(mView.getEdContent().getText().toString());
        mView.getAddressBean().setTypeCode(typeCode[mIntItem]);
        mView.getAddressBean().setTypeName(addtype[mIntItem]);
        getCompile();
    }

    //编辑新增（id为空表示新增）接口请求
    public void getCompile() {
        mView.LoadDialogShow();
        Params params = new Params();
        params.add("address", mView.getAddressBean().getAddress());
        params.add("addressDetail", mView.getAddressBean().getAddressDetail());
        params.add("addressMap", mView.getAddressBean().getAddressMap());
        params.add("area", mView.getAddressBean().getArea());
        Log.i(TAG,"getCompile ==> getArea = "+mView.getAddressBean().getArea());
        params.add("city", mView.getAddressBean().getCity());
        Log.i(TAG,"getCompile ==> getCity = "+mView.getAddressBean().getCity());
        params.add("id", mView.getAddressBean().getId());
        params.add("isDefault", mView.getAddressBean().getIsDefault());
        params.add("lat", mView.getAddressBean().getLat());
        params.add("lon", mView.getAddressBean().getLon());
        params.add("province", mView.getAddressBean().getProvince());
        params.add("typeCode", mView.getAddressBean().getTypeCode());
        params.add("typeName", mView.getAddressBean().getTypeName());
        mAuz.auzpost(UrlPool.POST_ADDRESS_UPDATE, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    if ("compile".equals(mView.getIsAddOrCompile())) {
                        Toast toast = Toast.makeText(mContext, "编辑成功", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                    } else {
                        Toast toast = Toast.makeText(mContext, "添加成功", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                    }
                    mView.onSuccess();
                } else {
                    mView.onCompile();
                    mView.LoadDialogdismiss();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
                mView.LoadDialogdismiss();
            }
        });
    }

    public void setTypeCode(String typeCode) {
        if (StateUtils.XIAO_QU.equals(typeCode)) {
            mIntItem = 0;
        } else if (StateUtils.XIE_ZI_LOU.equals(typeCode)) {
            mIntItem = 1;
        } else if (StateUtils.XUE_XIAO.equals(typeCode)) {
            mIntItem = 2;
        } else if (StateUtils.QI_TA.equals(typeCode)) {
            mIntItem = 3;
        }
    }

    private void checkType(String checktype) {
        if (StateUtils.XIAO_QU.equals(checktype)) {
            mView.getLabel1().setBackgroundResource(R.drawable.shap_client_tag_default);
            mView.getLabel2().setBackgroundResource(R.drawable.shap_client_tag_bg);
            mView.getLabel3().setBackgroundResource(R.drawable.shap_client_tag_bg);
            mView.getLabel4().setBackgroundResource(R.drawable.shap_client_tag_bg);
        } else if (StateUtils.XIE_ZI_LOU.equals(checktype)) {
            mView.getLabel1().setBackgroundResource(R.drawable.shap_client_tag_bg);
            mView.getLabel2().setBackgroundResource(R.drawable.shap_client_tag_default);
            mView.getLabel3().setBackgroundResource(R.drawable.shap_client_tag_bg);
            mView.getLabel4().setBackgroundResource(R.drawable.shap_client_tag_bg);
        } else if (StateUtils.XUE_XIAO.equals(checktype)) {
            mView.getLabel1().setBackgroundResource(R.drawable.shap_client_tag_bg);
            mView.getLabel2().setBackgroundResource(R.drawable.shap_client_tag_bg);
            mView.getLabel3().setBackgroundResource(R.drawable.shap_client_tag_default);
            mView.getLabel4().setBackgroundResource(R.drawable.shap_client_tag_bg);
        } else if (StateUtils.QI_TA.equals(checktype)) {
            mView.getLabel1().setBackgroundResource(R.drawable.shap_client_tag_bg);
            mView.getLabel2().setBackgroundResource(R.drawable.shap_client_tag_bg);
            mView.getLabel3().setBackgroundResource(R.drawable.shap_client_tag_bg);
            mView.getLabel4().setBackgroundResource(R.drawable.shap_client_tag_default);
        }
    }
}
